package org.pentaho.kettle.plugin.mqtt.steps.producer;


import java.util.List;
import org.pentaho.di.core.CheckResult;
import org.pentaho.di.core.CheckResultInterface;
import org.pentaho.di.core.annotations.Step;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.exception.KettleStepException;
import org.pentaho.di.core.exception.KettleValueException;
import org.pentaho.di.core.exception.KettleXMLException;
import org.pentaho.di.core.row.RowMetaInterface;
import org.pentaho.di.core.row.ValueMeta;
import org.pentaho.di.core.row.ValueMetaInterface;
import org.pentaho.di.core.variables.VariableSpace;
import org.pentaho.di.core.xml.XMLHandler;
import org.pentaho.di.i18n.BaseMessages;
import org.pentaho.di.repository.ObjectId;
import org.pentaho.di.repository.Repository;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;
import org.pentaho.di.trans.step.BaseStepMeta;
import org.pentaho.di.trans.step.StepDataInterface;
import org.pentaho.di.trans.step.StepInterface;
import org.pentaho.di.trans.step.StepMeta;
import org.pentaho.di.trans.step.StepMetaInterface;
import org.pentaho.kettle.plugin.mqtt.DialogClassUtil;
import org.pentaho.metastore.api.IMetaStore;
import org.w3c.dom.Node;

@Step(
        id = "mqttProducer",
        image = "org/pentaho/kettle/plugin/mqtt/steps/producer/producer.svg",
        i18nPackageName = "org.pentaho.kettle.plugin.mqtt.steps.producer",
        name = "mqttProducer.Name",
        description = "mqttProducer.TooltipDesc",
        categoryDescription = "i18n:org.pentaho.di.trans.step:BaseStep.Category.Transform"
)
public class ProducerStepMeta extends BaseStepMeta implements StepMetaInterface {
    private static Class<?> PKG = ProducerStepMeta.class;
    private String pubBroker;
    private String pubMessage;
    private String pubTopic;
    private String pubClientId;
    private int pubQos;
    public static final String DIALOG_NAME;

    public ProducerStepMeta() throws Throwable {
    }

    public StepInterface getStep(StepMeta stepMeta, StepDataInterface stepDataInterface, int cnr, TransMeta transMeta, Trans disp) {
        return new ProducerStep(stepMeta, stepDataInterface, cnr, transMeta, disp);
    }

    public StepDataInterface getStepData() {
        return new ProducerStepData();
    }

    public void setDefault() {
        this.pubBroker = "tcp://127.0.0.1:1883";
        this.pubMessage = "";
        this.pubTopic = "iotdevices";
        this.pubClientId = "client_id";
        this.pubQos = 1;
    }

    public void setPubBroker(String pubBroker) {
        this.pubBroker = pubBroker;
    }

    public void setPubClientId(String pubClientId) {
        this.pubClientId = pubClientId;
    }

    public void setPubMessage(String pubMessage) {
        this.pubMessage = pubMessage;
    }

    public void setPubQos(int pubQos) {
        this.pubQos = pubQos;
    }

    public void setPubTopic(String pubTopic) {
        this.pubTopic = pubTopic;
    }

    public String getPubBroker() {
        return this.pubBroker;
    }

    public String getPubClientId() {
        return this.pubClientId;
    }

    public String getPubMessage() {
        return this.pubMessage;
    }

    public String getPubTopic() {
        return this.pubTopic;
    }

    public int getPubQos() {
        return this.pubQos;
    }

    public Object clone() {
        Object retval = super.clone();
        return retval;
    }

    public String getXML() throws KettleValueException {
        StringBuffer retval = new StringBuffer(300);
        retval.append("    ").append(XMLHandler.addTagValue("pubBroker", this.getPubBroker()));
        retval.append("    ").append(XMLHandler.addTagValue("pubMessage", this.getPubMessage()));
        retval.append("    ").append(XMLHandler.addTagValue("pubTopic", this.getPubTopic()));
        retval.append("    ").append(XMLHandler.addTagValue("pubClientId", this.getPubClientId()));
        retval.append("    ").append(XMLHandler.addTagValue("pubQOS", this.getPubQos()));
        return retval.toString();
    }

    public void loadXML(Node stepnode, List<DatabaseMeta> databases, IMetaStore metaStore) throws KettleXMLException {
        try {
            this.setPubBroker(XMLHandler.getNodeValue(XMLHandler.getSubNode(stepnode, "pubBroker")));
            this.setPubClientId(XMLHandler.getNodeValue(XMLHandler.getSubNode(stepnode, "pubClientId")));
            this.setPubMessage(XMLHandler.getNodeValue(XMLHandler.getSubNode(stepnode, "pubMessage")));
            this.setPubTopic(XMLHandler.getNodeValue(XMLHandler.getSubNode(stepnode, "pubTopic")));
            this.setPubQos(Integer.parseInt(XMLHandler.getNodeValue(XMLHandler.getSubNode(stepnode, "pubQOS"))));
        } catch (Exception var5) {
            throw new KettleXMLException("Demo plugin unable to read step info from XML node", var5);
        }
    }

    public void saveRep(Repository rep, IMetaStore metaStore, ObjectId id_transformation, ObjectId id_step) throws KettleException {
        try {
            rep.saveStepAttribute(id_transformation, id_step, "pubBroker", this.pubBroker);
            rep.saveStepAttribute(id_transformation, id_step, "pubMessage", this.pubMessage);
            rep.saveStepAttribute(id_transformation, id_step, "pubTopic", this.pubTopic);
            rep.saveStepAttribute(id_transformation, id_step, "pubClientId", this.pubClientId);
            rep.saveStepAttribute(id_transformation, id_step, "pubQOS", (long)this.pubQos);
        } catch (Exception var6) {
            throw new KettleException("Unable to save step into repository: " + id_step, var6);
        }
    }

    public void readRep(Repository rep, IMetaStore metaStore, ObjectId id_step, List<DatabaseMeta> databases) throws KettleException {
        try {
            this.pubBroker = rep.getStepAttributeString(id_step, "pubBroker");
            this.pubMessage = rep.getStepAttributeString(id_step, "pubMessage");
            this.pubTopic = rep.getStepAttributeString(id_step, "pubTopic");
            this.pubClientId = rep.getStepAttributeString(id_step, "pubClientId");
            this.pubQos = (int)rep.getStepAttributeInteger(id_step, "pubQOS");
        } catch (Exception var6) {
            throw new KettleException("Unable to load step from repository", var6);
        }
    }

    public void getFields(RowMetaInterface inputRowMeta, String name, RowMetaInterface[] info, StepMeta nextStep, VariableSpace space, Repository repository, IMetaStore metaStore) throws KettleStepException {
        ValueMetaInterface v = new ValueMeta();
        v.setTrimType(3);
        v.setOrigin(name);
    }

    public void check(List<CheckResultInterface> remarks, TransMeta transMeta, StepMeta stepMeta, RowMetaInterface prev, String[] input, String[] output, RowMetaInterface info, VariableSpace space, Repository repository, IMetaStore metaStore) {
        CheckResult cr;
        if (input.length > 0) {
            cr = new CheckResult(1, BaseMessages.getString(PKG, "Demo.CheckResult.ReceivingRows.OK", new String[0]), stepMeta);
            remarks.add(cr);
        } else {
            cr = new CheckResult(4, BaseMessages.getString(PKG, "Demo.CheckResult.ReceivingRows.ERROR", new String[0]), stepMeta);
            remarks.add(cr);
        }

    }

    public String getDialogClassName() {
        return DIALOG_NAME;
    }

    static {
        DIALOG_NAME = DialogClassUtil.getDialogClassName(PKG);
    }
}
